
const moment = require("../../miniprogram_npm/moment/index.js")
const Protocol = require("../../Controls/Protocol")
const Product = require("../../model/Product")

var data =
{
  currentPage: 1,
  maxPage: 1,
  info:
  {
    indicatorDots: true,
    autoPlay: true,
    interval: false,
    duration: 300,
    empty_txt: "没有发布任何秘袋",
    ingProduct:
    [
      //{ name: "产品ing1", images: ["../../image/yellowchild.jpg", "../../image/1.jpg"] },
      //{ name: "产品ing2", images: ["../../image/yellowchild.jpg", "../../image/1.jpg"] },
      //{ name: "产品ing3", images: ["../../image/yellowchild.jpg", "../../image/1.jpg"] },
      //{ name: "产品ing4", images: ["../../image/yellowchild.jpg"] },
    ],

    otherProduct:
    [
      //{ name: "名字1", images: ["../../image/kefu.png", "../../image/1.jpg"] },
      //{ name: "名字2", images: ["../../image/kefu.png", "../../image/1.jpg"] },
      //{ name: "名字3", images: ["../../image/kefu.png", "../../image/1.jpg"] },
    ]
  }
}

function onSuccess(that, res)
{
  var app = getApp();

  data.currentPage = res.data.data.page;
  data.maxPage = res.data.data.maxPage;

  var ing = new Array();
  var other = new Array();

  for(var i = 0; i < res.data.data.items.length; i++)
  {
    var item = res.data.data.items[i];
    var viewItem = Product.newProduct(item);
    var now = moment();
    if (now > viewItem.expriceTime) {
      other.push(viewItem);
    }
    else {
      ing.push(viewItem);
      console.log(item.images);
    }
  }

  var newInfo = 
  {
    indicatorDots: true,
    autoPlay: true,
    interval: false,
    duration: 300,
    empty_txt: "没有发布任何秘袋",
    ingProduct: ing,
    otherProduct: other
  };

  that.setData({info:newInfo});
}

function onFail(that, res)
{
  console.error(res);
}

function onComplete(that, res)
{
  console.log(res);
}


var onLoad = function (args) {

  var opt = 
  {
    success:(res)=>{onSuccess(this, res)},
    fail:(res)=>{onFail(this, res)},
    complete:(res)=>{this, res}
  };

  Protocol.myPublished(1, opt);
}

function onClickIngitemIvk(event)
{
  var id = event.currentTarget.dataset.item.id;
  wx.navigateTo({ url: `../../pages/product/product?id=${id}`})
}

function onClickItemIvk(event) {
  console.log(event.currentTarget.dataset.item.name);
  var info = this.data.info;
  info.currentProduct = event.currentTarget.dataset.item;
  info.indicatorDots = false;

  this.setData({ info: info });
}

var options =
{
  data,
  onLoad,
  onClickItemIvk,
  onClickIngitemIvk,
}

Page(options)